<?php

namespace App\Kuafu\Complain\Controller\Applet;

use App\Exceptions\BaseException;
use App\Kuafu\Base\Enum\BaseCodeEnum;
use App\Kuafu\Complain\Service\ComplainService;
use App\Kuafu\Complain\Validate\ComplainValidate;
use App\Kuafu\Customer\Service\CustomerPlatformService;
use App\Kuafu\User\Enum\UserCodeEnum;
use App\Kuafu\User\Exceptions\UserException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use App\Kuafu\Base\Controller\BaseController;

class ComplainController extends BaseController
{

    /**
     * 新增投诉
     * @param Request $request
     * @return JsonResponse
     * @throws BaseException
     */
    public function add(Request $request): JsonResponse
    {
        $userId   = CustomerPlatformService::getUserId($request);
        //参数校验
        $valid = ComplainValidate::addComplain($request);
        if ($valid) {
            return $this->fail(BaseCodeEnum::ERR_400000, $valid);
        }
        if (!preg_match("/^1[3456789]\d{9}$/", $request->phone)) {
            throw new UserException(UserCodeEnum::ERR_USER_MOBILE);
        }
        $shopId = (int)$request->get('shopId', 0);//新获取shopId方式
        if (!$shopId) {//兼容之前的获取方式
            $shopId = (int)$request->header("shopId");
        }
        if (!$shopId) {
            return $this->fail(BaseCodeEnum::ERR_400000, 'shopId不能为空');
        }
        ComplainService::addComplain($request, $shopId, $userId);
        return $this->success();
    }

    /**
     * 获取小程序投诉问题列表
     * @return JsonResponse
     */
    public function issueList(): JsonResponse
    {
        return $this->success(ComplainService::getIssueList());
    }
}
